<?php
require './top/TopClient.php';
require './top/RequestCheckUtil.php';
require './top/Logger.php';
require './top/request/ItemGetRequest.php';
require './top/request/ItemcatsGetRequest.php';
require './top/request/TaobaokeItemsConvertRequest.php';

class TaoBaoAction extends Action {
    var $PickRoot= "";
    var $TopClient =null;

    var $AppKey="21087743";
    var $SecretKey ="5f0cf1f8200d4854cb87410620878159";
    var $Nick="douqin2010";
    var $Pid=32295788;
    
    function __construct(){
    	load("@.dom");
        $this->PickRoot=PICK_DIR.MODULE_NAME.DIRECTORY_SEPARATOR;
        if(!is_dir($this->PickRoot)){
            mkdir($this->PickRoot);
        }
       
        $this->TopClient = new TopClient();
        $this->TopClient->appkey = $this->AppKey;
        $this->TopClient->secretKey = $this->SecretKey;
        $this->TopClient->format="json";
        
    }
    
    function __destruct(){
      
    }
    
    public function cats(){
    	sleep(1);
    	$parentid=intval($_GET['parentid']);
    	$listName=$_GET['name'];
    	
    	
    	
    	$ItemcatsGetRequest = new ItemcatsGetRequest;
    	$ItemcatsGetRequest->setFields("cid,parent_cid,name,is_parent");
    	$ItemcatsGetRequest->setParentCid($parentid);
    	$TBCache = $this->TopClient->execute($ItemcatsGetRequest);    	
    	file_put_contents($this->PickRoot."cats_".$parentid.".txt", print_r($TBCache,true)) ;
    	if(!empty($TBCache->item_cats->item_cat)){
	    	$ItemCat= $TBCache->item_cats->item_cat;
	    	logCmd("   ".$parentid." => ".$listName." => ". Count($ItemCat));
	    	foreach ($ItemCat as $cat){
	    	
// 	    		$name = iconv("utf-8", "gbk", $cat->name);
	    		
// 	    		if($parentid == 0){
// 	    			$listName ="";
// 	    			$listName = $name;
// 	    		}else{
	    			
// 	    			$listName = $listName."-".$name;
	    			
// 	    		}
	
	    		$listName = str_replace("\\", ".",$listName);
	    		$listName = str_replace("/", ".",$listName);
	    		
	    		CmdExec("TaoBao cats parentid ".$cat->cid ." name ".$parentid);
	    	}
    	}else{
    		logCmd("   ".$parentid." => ".$listName." => 0" );
    	}
    }
    
    
    public function index(){
    
    	
    	
    }
    
    public function shop(){
    	$shopUrl = "http://handuyishe.tmall.com/";
    	$itme = file_get_html($shopUrl."search.htm?search=y&viewType=list&orderType=_hotsell&pageNum=1");
    	$Pages= substr($itme->find(".page-info",0)->innertext(), 2);
    	for ($page = 1; $page <= $Pages; $page++) {
    		$itme = file_get_html($shopUrl."search.htm?search=y&viewType=list&orderType=_hotsell&pageNum=".$page);
    		foreach ($itme->find(".shop-hesper-bd .item ") as $k=>$it){
    			$url = parse_url($it->find("a",1)->href);
    			parse_str($url['query'], $urlout);
    			$CacheId =($urlout['id']);
    			CmdExec("TaoBao item tbid ".$CacheId);    			
    			sleep(1);
    			 
    		}
    	}
    	
    }
    
    
    public function search(){
    	
    }
    /**
     * 根据淘宝id 获取淘宝信息
     */
    public function item(){
    	$CacheId = $_GET['tbid'];
    	$ItemGetRequest = new ItemGetRequest;
    	$ItemGetRequest->setFields("detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual");
    	$ItemGetRequest->setNumIid($CacheId);
    	$TBCache = $this->TopClient->execute($ItemGetRequest);
    	print_r($TBCache);
    }
    /**
     * 淘宝客转换
     */
    public function convert(){
    	$CacheId = $_GET['tbid'];
    	$TaobaokeItemsConvertRequest = new TaobaokeItemsConvertRequest();
    	$TaobaokeItemsConvertRequest->setFields(
    			"click_url,num_iid,commission,commission_rate,commission_num,commission_volume");
    	$TaobaokeItemsConvertRequest->setNick($this->Nick);
    	$TaobaokeItemsConvertRequest->setPid($this->Pid);    	
    	$TaobaokeItemsConvertRequest->setNumIids($CacheId);
    	
    	$TBCache = $this->TopClient->execute($TaobaokeItemsConvertRequest);
    	
    	
    	print_r($TBCache);
    }
}